package v1

import (
	"dolphin/sports/pkg/util"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/gjson"
)

// 返回所有可用用户的姓名
func UsersName(c *gin.Context) {

}

// 获取用户登录信息
func UserInfo(c *gin.Context) {

	token := c.Query("token")

	if token == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "token不能为空"})
		return
	}

	userinfo, err := util.GetUserInfo(token)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("userinfo: ", string(userinfo))

	name := gjson.GetBytes(userinfo, "data.name")
	user_id := gjson.GetBytes(userinfo, "data.id")
	nickname := gjson.GetBytes(userinfo, "data.nickname")

	if gjson.GetBytes(userinfo, "code").Int() == 0 {
		c.JSON(http.StatusOK, gin.H{
			"name":     name.String(),
			"nickname": nickname.String(),
			"user_id":  user_id.Int(),
			"avatar":   "https://img1.3lian.com/2015/w7/85/d/101.jpg",
			"access":   "",
		})
		return
	} else {
		c.JSON(http.StatusBadRequest, gin.H{"error": "获取用户信息失败"})
		return
	}
}
